﻿<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php include("includes/header.php"); ?>

<?php //check if "data" set if set get array to $personalas
	if (!isset($_GET['data'])) {
		$to = "client.php";
		redirect_to($to);
		exit;  
	};
	//POST process
if (isset($_POST['add'])) {
		
	$query="INSERT INTO `skirstimassudetis` (`svarstdata` ,`dalyvisid` ,`tipas`) VALUES (";
	$query.="'";
	$query.=($_GET['data']);
	$query.="', '";
	$query.=($_POST['dalyvis']);
	$query.="', '3');";
	$add = mysql_query($query, $connection);
	confirm_query($add);
};
if (isset($_POST['submit'])) {
	
	$query="UPDATE `skirstimassudetis` SET `dalyvisid` = '";
	$query.=($_POST['pirmininkas']); 
	$query.="' WHERE `skirstimassudetis`.`skirstimassudetisid` =";
	$query.=($_POST['pirmval']);
	$query.=" LIMIT 1 ;";
	$update = mysql_query($query, $connection);
	confirm_query($update);
	
	$query="UPDATE `skirstimassudetis` SET `dalyvisid` = '";
	$query.=($_POST['sekretorius']); 
	$query.="' WHERE `skirstimassudetis`.`skirstimassudetisid` =";
	$query.=($_POST['sekrval']);
	$query.=" LIMIT 1 ;";
	$update = mysql_query($query, $connection);
	confirm_query($update);
	
	$to = "form_preprot.php?data=";
	$to .=($_GET['data']);
		redirect_to($to);
		exit;  
	
};
	//check ar yra ka trinti
	if (isset($_GET['del'])){
	$query = "DELETE FROM `skirstimassudetis` WHERE `skirstimassudetis`.`skirstimassudetisid` = ";
	$query .=($_GET['del']);
	$query .=" LIMIT 1";
	$delete = mysql_query($query, $connection);
	confirm_query($delete);
	};	
	//o toliau "po svezhe utverzhdennomu planu" (C)k/f "Briliantovaja ruka"
	$query = "SELECT * FROM `skirstimassudetis` WHERE svarstdata='";
	$query .=($_GET['data']);
	$query .="'";
	$query .=" LIMIT 1";
	$test_set = mysql_query($query, $connection);
	confirm_query($test_set);
	$test_count = mysql_affected_rows($connection);
	//echo $test_count;
	if ($test_count ==0){
	//FIX
		$query = "SELECT * FROM `skirstimassudetis` WHERE ";
		$query .=" `tipas` = 1";
		$query .=" ORDER BY `svarstdata` DESC ";
		$query .=" LIMIT 1";
		$findlatest_set = mysql_query($query, $connection);
		confirm_query($findlatest_set);
		$findlatest = mysql_fetch_array($findlatest_set);
		//echo $findlatest['svarstdata'];
		
		$query = "SELECT * FROM `skirstimassudetis` WHERE svarstdata='";
		$query .=($findlatest['svarstdata']);
		$query .="'";
		$fix_set = mysql_query($query, $connection);
		confirm_query($fix_set);
		$test_count = mysql_affected_rows($connection);
		while ($fix = mysql_fetch_assoc($fix_set)){
		//echo "boo";
		//echo $c++;
		//insert some shit
		$query = "INSERT INTO `skirstimassudetis` (`svarstdata` ,`dalyvisid` ,`tipas`) VALUES (";
		$query .= "'" ;
		$query .=  ($_GET['data']);
		$query .= "'," ;
		$query .= ($fix['dalyvisid']);
		$query .=  ",";
		$query .= ($fix['tipas']);
		$query .= ");" ;
		$insert = mysql_query($query, $connection);
		confirm_query($insert);
		}; 
	};

	//Pirmininkas
	$query = "SELECT * FROM `skirstimassudetis` WHERE svarstdata='";
	$query .=($_GET['data']);
	$query .="'";
	$query .=" AND `tipas` = 1";
	$query .=" LIMIT 1";
	$pirmininkas_set = mysql_query($query, $connection);
	$pirmininkas_count = mysql_affected_rows($connection);
	//echo $pirmininkas_count;
	if ($pirmininkas_count ==1){
		confirm_query($pirmininkas_set);
		$pirmininkas = mysql_fetch_array($pirmininkas_set);
	}else{
		$query = "SELECT * FROM `skirstimassudetis` WHERE ";
		$query .=" `tipas` = 1";
		$query .=" ORDER BY `svarstdata` DESC ";
		$query .=" LIMIT 1";
		$pirmininkas_set = mysql_query($query, $connection);
		confirm_query($pirmininkas_set);
		$pirmininkas = mysql_fetch_array($pirmininkas_set);
	};
	
	
	//sekretorius
	$query = "SELECT * FROM `skirstimassudetis` WHERE svarstdata='";
	$query .=($_GET['data']);
	$query .="'";
	$query .=" AND `tipas` = 2";
	$query .=" LIMIT 1";
	$sekretorius_set = mysql_query($query, $connection);
	$sekretorius_count = mysql_affected_rows($connection);
	//echo $sekretorius_count;
	if ($sekretorius_count ==1){
		confirm_query($sekretorius_set);
		$sekretorius = mysql_fetch_array($sekretorius_set);
	}else{
		$query = "SELECT * FROM `skirstimassudetis` WHERE ";
		$query .=" `tipas` = 2";
		$query .=" ORDER BY `svarstdata` DESC ";
		$query .=" LIMIT 1";
		$sekretorius_set = mysql_query($query, $connection);
		confirm_query($sekretorius_set);
		$sekretorius = mysql_fetch_array($sekretorius_set);
	};

	
?>


<article>
	<h2>skirstimassudetis komisijos posėdžio protokolo formavimas</h2>
	<form action="skirstimassudetis.php?data=<?php 
	echo ($_GET['data']);
	?>" method="post">
	Posėdžio pirmininkas:<select name="pirmininkas">
	  <?php 		$query = "SELECT * 
						FROM `personalas` 
						ORDER BY personalasid ASC";
						$personalas_set = mysql_query($query, $connection);
						confirm_query($personalas_set);
						while ($personalas = mysql_fetch_array($personalas_set)) {
							echo "<option value=\"". $personalas["personalasid"] . "\"";
							if (($personalas["personalasid"])==($pirmininkas["dalyvisid"])){
								echo " selected=\"selected\" ";
							};
							echo ">" . $personalas["vardas"] ." ".$personalas["pavarde"];
							echo "</option>";
						};

			?>
	</select><input name="pirmval" type="hidden" value="<?php echo ($pirmininkas["skirstimassudetisid"]);?>">
	<br>	<br>
	Posėdžio sekretorius:<select name="sekretorius">
	  <?php 		$query = "SELECT * 
						FROM `personalas` 
						ORDER BY personalasid ASC";
						$personalas_set = mysql_query($query, $connection);
						confirm_query($personalas_set);
						while ($personalas = mysql_fetch_array($personalas_set)) {
							echo "<option value=\"". $personalas["personalasid"] . "\"";
							if (($personalas["personalasid"])==($sekretorius["dalyvisid"])){
								echo " selected=\"selected\" ";
							};
							echo ">" . $personalas["vardas"] ." ".$personalas["pavarde"]."</option>";
						};

			?>
	</select><input name="sekrval" type="hidden" value="<?php echo ($sekretorius["skirstimassudetisid"]);?>">
	<br>	<br>
	Dalyvauja:	<br>
	<table  border="0" cellspacing="4" cellpadding="0">
 <?php 			
	$query = "SELECT * 
	FROM `personalas`, `skirstimassudetis`
	WHERE `skirstimassudetis`.`svarstdata` ='";
	$query .= ($_GET['data']);
	$query .= "' AND `skirstimassudetis`.`dalyvisid`=";
	$query .= "`personalas`.`personalasid`";
	$query .= " AND `skirstimassudetis`.`tipas`= 3";
	$query .= " ORDER BY personalasid ASC";
	$personalas_set = mysql_query($query, $connection);
	confirm_query($personalas_set);
	
	while ($personalas = mysql_fetch_array($personalas_set)) {
		echo "<tr>
	    <td>";
		
		echo "<a href=\"skirstimassudetis.php?data=";
			echo ($_GET['data']);
			echo "&del=";
			echo $personalas['skirstimassudetisid'];
			echo "\">[-]</a>";
		
		echo "</td>
	    <td>";
		// echo "<option value=\"". $personalas["personalasid"] . "\"";
		// if (($personalas["personalasid"])==($sekretorius["dalyvisid"])){
			// echo " selected=\"selected\" ";
		// };
		echo $personalas["vardas"] ." ".$personalas["pavarde"];
		echo "</td>
      </tr>";
	};
?>
	  
	  
	  
	  <tr>
	    <td align="center"><?php 
		if (!isset($_GET['add'])){
			echo "<a href=\"skirstimassudetis.php?data=";
			echo ($_GET['data']);
			echo "&add=1\">[+]</a>";
		};
		?></td>
	    <td>&nbsp;</td>
      </tr>
	  </table><?php 
		if (isset($_GET['add'])){
			echo "<select name=\"dalyvis\">";
						$query = "SELECT * 
						FROM `personalas` 
						ORDER BY personalasid ASC";
						$personalas_set = mysql_query($query, $connection);
						confirm_query($personalas_set);
						while ($personalas = mysql_fetch_array($personalas_set)) {
							echo "<option value=\"". $personalas["personalasid"] . "\"";
							echo ">" . $personalas["vardas"] ." ".$personalas["pavarde"]. "</option>";
						};

			echo "</select>";
			echo "<input name=\"add\" type=\"submit\" value=\"Pridet\" />";
		};
		?>
      
	<br><br><input name="submit" type="submit" value="Patvirkinti"/>
	</form>
		
</article>
<?php require("includes/footer.php"); ?>